package com.ruoyi.jobservice.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SProvinceCityEntity;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SProvinceCityMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISProvinceCityService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jobservice.mapper.SCompanyMapper;
import com.ruoyi.jobservice.domain.SCompany;
import com.ruoyi.jobservice.service.ISCompanyService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用工单位信息Service业务层处理
 * 
 * @author jeffery
 * @date 2021-08-31
 */
@Service
public class SCompanyServiceImpl implements ISCompanyService 
{
    @Autowired
    private SCompanyMapper sCompanyMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SProvinceCityMapper provinceCityMapper;

    /**
     * 查询用工单位信息
     * 
     * @param companyId 用工单位信息主键
     * @return 用工单位信息
     */
    @Override
    public SCompany selectSCompanyByCompanyId(Long companyId)
    {
        return sCompanyMapper.selectSCompanyByCompanyId(companyId);
    }

    /**
     * 查询用工单位信息列表
     * 
     * @param sCompany 用工单位信息
     * @return 用工单位信息
     */
    @Override
    public List<SCompany> selectSCompanyList(SCompany sCompany)
    {
        List<SCompany> companyList =  sCompanyMapper.selectSCompanyList(sCompany);
        List<SProvinceCityEntity> list = provinceCityMapper.selectList(null);

        Map<Integer, String> provinceCityMap = list.stream().collect(Collectors.toMap(SProvinceCityEntity::getCode, SProvinceCityEntity::getName));
        companyList.stream().forEach(x ->{
            x.setProvinceName(provinceCityMap.get(x.getProvinceId()));
            x.setCityName(provinceCityMap.get(x.getCityId()));
        });
        return companyList;
    }

    /**
     * 新增用工单位信息
     * 
     * @param sCompany 用工单位信息
     * @return 结果
     */
    @Override
    public int insertSCompany(SCompany sCompany)
    {
        sCompany.setCreateTime(DateUtils.getNowDate());
        return sCompanyMapper.insertSCompany(sCompany);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSCompanyUser(SCompany sCompany) {
        SysUser sysUser = new SysUser();
        sysUser.setUserName(sCompany.getUserName());
        sysUser.setNickName(sCompany.getUserName());
        sysUser.setPassword(SecurityUtils.encryptPassword(sCompany.getPassword()));
        sysUser.setRoleId(sCompany.getRoleId());
        sysUser.setDeptId(sCompany.getDeptId());
        sysUser.setCreateBy(sCompany.getCreateName());
        sysUser.setCompanyId(sCompany.getCompanyId());
        int row = sysUserMapper.insertUser(sysUser);
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setRoleId(sCompany.getRoleId());
        sysUserRole.setUserId(sysUser.getUserId());
        sysUserRoleMapper.insertUserRole(sysUserRole);
        return row;
    }

    /**
     * 修改用工单位信息
     * 
     * @param sCompany 用工单位信息
     * @return 结果
     */
    @Override
    public int updateSCompany(SCompany sCompany)
    {
        sCompany.setUpdateTime(DateUtils.getNowDate());
        return sCompanyMapper.updateSCompany(sCompany);
    }

    /**
     * 批量删除用工单位信息
     * 
     * @param companyIds 需要删除的用工单位信息主键
     * @return 结果
     */
    @Override
    public int deleteSCompanyByCompanyIds(Long[] companyIds)
    {
        return sCompanyMapper.deleteSCompanyByCompanyIds(companyIds);
    }

    /**
     * 删除用工单位信息信息
     * 
     * @param companyId 用工单位信息主键
     * @return 结果
     */
    @Override
    public int deleteSCompanyByCompanyId(Long companyId)
    {
        return sCompanyMapper.deleteSCompanyByCompanyId(companyId);
    }

    @Override
    public List<SCompany> getAllCompanysInfo() {
        List<SCompany> list =  new ArrayList<>();
        if (SecurityUtils.getLoginUser().getUser().isAdmin()){
            return sCompanyMapper.selectAll();
        }else {
            SysUser sysUser = SecurityUtils.getLoginUser().getUser();
            if (sysUser.getCompanyId() != null){
                SCompany sCompany = sCompanyMapper.selectSCompanyByCompanyId(sysUser.getCompanyId());
                if(sCompany != null){
                    list.add(sCompany);
                }
            }
        }
        return list;
    }
}
